package forum.service;

import java.sql.ResultSet;

import forum.DBConnectie;
import forum.Filter;
import forum.FilterBack;
import forum.Utilities;
import forum.Variable;
import forum.wrapper.WrapRequest;

import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: Ivan
 * Date: 2010-7-8
 * Time: 15:37:20
 */
public class Reply {

    DBConnectie db = new DBConnectie(Variable.getDb(), Variable.getDbLogin(), Variable.getDbPassword());

    public String add(WrapRequest request) throws Exception{

            String start = request.getParameter("start");
            String forum_id = request.getParameter("forum_id");
            int lastReply_id = Integer.parseInt(request.getParameter("lastReply_id"));
            String reply_id = Integer.toString(lastReply_id + 1);

            String thread_id = request.getParameter("thread_id");
            String message = request.getParameter("message");
            message = Filter.filterAll(message);


            String user = request.getParameter("user");

            db.connect();

            db.query(
                    "INSERT INTO forum_message(forum_id,thread_id,reply_id,message,user,date_time) " +
                            "VALUES(\"" + forum_id +
                            "\",\"" + thread_id +
                            "\",\"" + reply_id +
                            "\",\"" + message +
                            "\",\"" + user +
                            "\",SYSDATE())");

            db.close();

            return  "index.jsp?page=message&forum_id=" + forum_id + "&thread_id=" + thread_id + "&start=" + start;
    }

     public String delete(WrapRequest request) throws Exception{

            HttpSession session = request.getSession(true);
            String sessionType = (String) session.getAttribute("type");

            String forum_id = request.getParameter("forum_id");
            String thread_id = request.getParameter("thread_id");
            String reply_id = request.getParameter("reply_id");
            String start = request.getParameter("start");

            if (sessionType.equals("Admin")) {

                db.connect();

                db.query("DELETE FROM forum_message WHERE forum_id=\"" + forum_id + "\" AND thread_id=\"" + thread_id + "\" AND reply_id=\"" + reply_id + "\"");

                db.close();
            }

            return  "index.jsp?page=message&forum_id=" + forum_id + "&thread_id=" + thread_id + "&start=" + start;
    }

    public String edit(WrapRequest request) throws Exception{

            HttpSession session = request.getSession(true);
            String sessionUsername = (String) session.getAttribute("username");
            String sessionType = (String) session.getAttribute("type");

            String forum_id = request.getParameter("forum_id");
            String start = request.getParameter("start");
            String reqThread_id = request.getParameter("thread_id");
            String reqReply_id = request.getParameter("reply_id");
            String message = request.getParameter("message");
            message = Filter.filterAll(message);

            int changeDifference = (((100 * message.length()) / Utilities.getMessageLength(forum_id, reqThread_id, reqReply_id)));

            java.util.Date date_time = new java.util.Date();

            message += "------- begin --------<BR><BR><I>Edited by " + sessionUsername + " - " + date_time + " (" + changeDifference + "%)</I><!-- end --!>";

            db.connect();

            if (sessionType.equals("Admin")) {
                db.query(
                        "UPDATE forum_message " +
                                "SET message =\"" + message + "\"" +
                                "WHERE forum_id=\"" + forum_id + "\" AND thread_id =\"" + reqThread_id + "\" AND reply_id=\"" + reqReply_id + "\"");
            } else {
                db.query(
                        "UPDATE forum_message " +
                                "SET message =\"" + message + "\"" +
                                "WHERE forum_id=\"" + forum_id + "\" AND thread_id =\"" + reqThread_id + "\" AND reply_id=\"" + reqReply_id + "\" AND user=\"" + sessionUsername + "\"");
            }
            
            ResultSet rs = db.selectQuery(
                    "SELECT * FROM forum_message " +
                            "WHERE forum_id=\"" + forum_id + "\" AND thread_id =\"" + reqThread_id + "\" AND reply_id=\"" + reqReply_id + "\"");

            String msg = "";
            while (rs.next()) {
            	msg = rs.getString("message");

                if (msg.indexOf("<!-- begin --!>") != -1) {
                    int begin = msg.indexOf("<!-- begin --!>");
                    msg = msg.substring(0, begin);
                }
            }

            msg = FilterBack.filterAll(msg);

            db.close();

            return  "index.jsp?page=message&forum_id=" + forum_id + "&thread_id=" + reqThread_id + "&start=" + start + "&message=" + msg;
    }
}
